package com.ifacebox.face.track.ui;

import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamPanel;
import com.github.sarxos.webcam.WebcamResolution;

import javax.swing.*;
import java.awt.*;
import java.util.Arrays;

/**
 * @author znn
 */
public class FaceWebCamPopupMenu extends JPopupMenu {
    public FaceWebCamPopupMenu(Webcam webCam, WebcamPanel webCamPanel) {
        super.add(new JCheckBoxMenuItem("镜像") {{
            this.addActionListener((e) -> webCamPanel.setMirrored(this.isSelected()));
        }});
        JMenu drawModeMenu = new JMenu("画面");
        ButtonGroup drawModeGroup = new ButtonGroup();
        for (WebcamPanel.DrawMode drawMode : Arrays.asList(WebcamPanel.DrawMode.FIT, WebcamPanel.DrawMode.FILL)) {
            JCheckBoxMenuItem drawModeItem = new JCheckBoxMenuItem(drawMode.name(), WebcamPanel.DrawMode.FIT == drawMode);
            drawModeItem.addActionListener((e) -> webCamPanel.setDrawMode(drawMode));
            drawModeGroup.add(drawModeItem);
            drawModeMenu.add(drawModeItem);
        }
        super.add(drawModeMenu);
        webCam.setCustomViewSizes(Arrays.stream(WebcamResolution.values())
                .map(WebcamResolution::getSize).toArray(Dimension[]::new));
        JMenu viewSizeMenu = new JMenu("分辨率");
        ButtonGroup viewSizeGroup = new ButtonGroup();
        for (Dimension viewSize : webCam.getCustomViewSizes()) {
            JCheckBoxMenuItem viewSizeItem = new JCheckBoxMenuItem(String.format("%dx%d"
                    , viewSize.width, viewSize.height), webCam.getViewSize().equals(viewSize));
            viewSizeItem.addActionListener((e) -> {
                webCamPanel.stop();
                webCam.close();
                webCam.setViewSize(viewSize);
                webCamPanel.start();
            });
            viewSizeGroup.add(viewSizeItem);
            viewSizeMenu.add(viewSizeItem);
        }
        super.add(viewSizeMenu);
    }
}
